<?php


namespace app\admin\controller;


use app\admin\model\CarOrder;
use app\admin\model\DistinguishLog;
use app\BaseController;
use app\Request;

class Order extends  BaseController
{
    public function getList(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where['manager_id'] = $this->userInfo['id'];
        }
        $store_id = $request->post('store_id',0);
        if(!empty($store_id)){
            $where['store_id'] = $store_id;
        }
        $manager_id = $request->post('manager_id',0);
        if(!empty($manager_id)){
            $where['manager_id'] = $manager_id;
        }
        $result = \app\api\model\Order::order('id desc')->where($where)->page($page, $limit)->with([
            'user' => function($query) {
                $query->field('id,wxname,wximage');
            }
        ])->select();
        $total  =  \app\api\model\Order::where($where)->count('id');
        //增加商户信息
        $result = \app\admin\model\Manager::getMananger($result);
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    public function getDistinguishLog(Request  $request)
    {
        $page = $request->post('page', 1);
        $limit = $request->post('limit', 10);
        $where = [];
        if(!empty($this->userInfo['username']) && $this->userInfo['username'] !='admin'){
            $where[] = ['manager_id','=',$this->userInfo['id']];
        }

        $license = $request->post('license', '');
        if(!empty($license)){
            $where[] = ['license','like',"%{$license}%"];
        }
        $store_id = $request->post('store_id',0);
        if(!empty($store_id)){
            $where[] = ['store_id','=',$store_id];
        }
        $manager_id = $request->post('manager_id',0);
        if(!empty($manager_id)){
            $where[] = ['manager_id','=',$manager_id];
        }

        $result = CarOrder::order('id desc')->where($where)->append(['pay_status'])->page($page, $limit)->with([
            'station' => function($query) {
                $query->field('id,name');
            },
            'joinUser' => function($query) {
                $query->field('id,wxname,wximage');
            },
            'outUser' => function($query) {
                $query->field('id,wxname,wximage');
            },
        ])->select();
        //增加商户信息
        $result = \app\admin\model\Manager::getMananger($result);
        $total  = CarOrder::where($where)->count();
        $data = [
            'total' => $total,
            'current' => $page,
            'perpage' => $limit,
            'data' =>  $result
        ];

        return show(config('status.success'), '操作成功', $data);
    }

    public function deleteCarOrder(Request  $request)
    {
        $order = CarOrder::where('id', $request->post('id'))->findOrEmpty();

        if (!$order->isEmpty()) {
            if ($request->post('status') == 1) {
                \app\admin\model\Station::where([
                    'id' => $order->station_id
                ])->update([
                    'status' => 0
                ]);
            }
            $order->delete();
        }
        return show(config('status.success'), '操作成功');
    }
}